<!DOCTYPE html>
<html lang="en"><head>
    <title>CZH-DEV BLOG</title>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="format-detection" content="telephone=no" />
    <meta name="theme-color" content="#000084" />
    <link rel="icon" href="https://czh-dev.gitee.io/czh-blog.gitee.io//favicon.ico">
    <link rel="canonical" href="https://czh-dev.gitee.io/czh-blog.gitee.io/">
    
    
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="navbar-inner">
        <div class="container">
            <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"></button>
            <a class="brand" href="https://czh-dev.gitee.io/czh-blog.gitee.io/">CZH-DEV BLOG</a>
            <div class="nav-collapse collapse">
                <ul class="nav">
                    
                    
                        
                            <li>
                                <a href="/czh-blog.gitee.io/about/">
                                    
                                    <span>About</span>
                                </a>
                            </li>
                        
                    
                        
                            <li>
                                <a href="/czh-blog.gitee.io/post/">
                                    
                                    <span>All posts</span>
                                </a>
                            </li>
                        
                    
                        
                            <li>
                                <a href="/czh-blog.gitee.io/ebook/">
                                    
                                    <span>Resource</span>
                                </a>
                            </li>
                        
                    
                </ul>
            </div>
        </div>
    </div>
</nav><div id="content" class="container">
<div style="display: flex;">
  <div class="row-fluid navmargin">
    <div class="page-header">
      <h1>Elasticsearch - Sat, Feb 11, 2023</h1>
    </div>
    <p class="lead"></p>
    <h1 id="elasticsearch">ElasticSearch</h1>
<h2 id="简介">简介</h2>
<p><strong>Elasticsearch</strong>是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎，基于RESTful web接口。Elasticsearch是用ava开发的，并作为Apachei许可条款下的开放源码发布，是当前流行的企业级搜索引擎。设计用于云计算中，能够达到实时搜索，稳定，可靠，快速，安装使用方便。</p>
<p>我们建立一个网站或应用程序，并要添加搜索功能，但是想要完成搜索工作的创建是非常困难的。我们希望搜索解决方案要运行速度快，我们希望能有一个零配置和一个完全免费的搜索模式，我们希望能够简单地使用SON通过HTTP来索引数据，我们希望我们的搜索服务器始终可用，我们希望能够从一台开始并扩展到数百台，我们要实时搜索，我们要简单的多用户，我们希望建立一个云的解决方案。因此我们利用Elasticsearch:来解决所有这些问题及可能出现的更多其它问题。</p>
<p><img src="https://czh-pic.oss-cn-guangzhou.aliyuncs.com/202302111224201.png" alt="image-20230101232058961"></p>
<p><strong>注意</strong>：从Elasticsearch6开始一个索引里面只能有一个类型，后续计划删除类型这个概念，从ES6开始一般让索引名称和类型名称一致</p>
<h3 id="主要组件">主要组件</h3>
<ul>
<li>
<p><strong>索引</strong></p>
<p>ES将数据存储于一个或多个索引中，索引是具有类似特性的文档的集合。类比传统的关系型数据库领域来说，索引相当于SQL中的一个数据库，或者一个数据存储方案(schem)。索引由其名称（必须为全小写字符)进行标识，并通过引用此名称完成文档的创建、搜索、更新及删除操作。一个ES集群中可以按需创健任意数目的索引。</p>
</li>
<li>
<p><strong>类型</strong></p>
<p>类型是索引内部的逻辑分区(category/partition),然而其意义完全取决于用户需求。因此，一个索引内部可定义一个或多个类型(ype,。一般来说，类型就是为那些拥有相同的域的文档做的预定义。例如，在索引中，可以定义一个用于存储用户数据的类型，一个存储日志数据的类型，以及一个存储评论数据的类型。类比传统的关系型数据库领域来说，类型相当于表。</p>
</li>
<li>
<p><strong>文档</strong></p>
<p>Mapping,就是对索引库中索引的字段名称及其数据类型进行定义，类似于mysql中的表结构信息。不过es的nappingh比数据库灵活很多，它可以动态识别字段。一般不需要指定mapping都可以，因为es会自动根据数据格式识别它的类型，如果你需要对某些字段添加特殊属性（如：定义使用其它分词器、是否分词、是否存储等)，就必须手动添加napping。.</p>
<p>需要注意的是映射是不可修改的，一旦确定就不允许改动，在使用自动识别功能时，会以第一个存入的文档为参考来建立映射，后面存入的文档也必须符合该映射才能存入</p>
</li>
</ul>
<h3 id="分片和副本">分片和副本</h3>
<p>ES的分片(shard)机制可将一个索引内部的数据分布地存储于多个节点，它通过将一个索引切分为多个底层物理的Lucene索引完成索引数据的分割存储功能，这每一个物理的Lucene索引称为一个分片(shad)。每个分片其内部都是一个全功能目独立的索引，因此可由集群中的任何主机存储。创建索引时，用户可指定其分片的数量，默认数量为5个。</p>
<p>Shard有两种类型：primary和replica,即主shard及副本shard。.Primary shard)用于文档存储，每个新的索引会自动创建5个Primary shard,当然此数量可在索引创建之前通过配置自行定义，不过，一旦创建完成，其Primary shard的数量将不可更改。Replica shard,是Primary Shard的副本，用于冗余数据及提高搜索性能。每个Primary shard默认配置了一个Replicashard,但也可以配置多个，且其数量可动态更改。ES会根据需要自动增加或减少这些Replica shard的数量。</p>
<h3 id="分词器">分词器</h3>
<p>把文本内容按照标准进行切分，默认的是standard,该分词器按照单词切分，内容转变为小写，去掉标点，遇到每个中文字符都当成1个单词处理，后面会安装开源的中文分词器插件(ik)</p>
<h2 id="操作索引相当于数据库">操作索引（相当于数据库）</h2>
<h3 id="添加索引">添加索引</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elm" data-lang="elm"><span style="display:flex;"><span><span style="color:#66d9ef">PUT</span> <span style="color:#960050;background-color:#1e0010">索引名</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;settings&#34;</span><span style="color:#a6e22e">:</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">&#34;number_of_shards&#34;</span><span style="color:#a6e22e">:</span> <span style="color:#ae81ff">5</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">&#34;number_of_replicas&#34;</span><span style="color:#a6e22e">:</span> <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p><code>&quot;number_of_shards&quot;: 5,</code>表示默认创建5个分片区</p>
<p><code>&quot;number_of_replicas&quot;: 1</code>表示默认备份一份</p>
<p><strong>注意：</strong></p>
<ul>
<li>索引不能有大写字母</li>
<li>参数格式必须要JSON格式</li>
<li>括号要成对出现</li>
<li>起名最好不要有”_“（下划线）开头</li>
</ul>
<p>如：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elm" data-lang="elm"><span style="display:flex;"><span><span style="color:#66d9ef">PUT</span> czh<span style="color:#ae81ff">20</span>
</span></span><span style="display:flex;"><span>{ 
</span></span><span style="display:flex;"><span>  <span style="color:#e6db74">&#34;settings&#34;</span><span style="color:#a6e22e">:</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;number_of_shards&#34;</span><span style="color:#a6e22e">:</span> <span style="color:#ae81ff">5</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;number_of_replicas&#34;</span><span style="color:#a6e22e">:</span> <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h3 id="查询索引">查询索引</h3>
<ul>
<li>
<p>查询<strong>czh20</strong>的索引（查询某个索引）</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elm" data-lang="elm"><span style="display:flex;"><span><span style="color:#66d9ef">GET</span> <span style="color:#a6e22e">/</span>czh<span style="color:#ae81ff">20</span>(<span style="color:#960050;background-color:#1e0010">索引名</span>)
</span></span></code></pre></div></li>
<li>
<p>查询<strong>全部</strong>索引</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elm" data-lang="elm"><span style="display:flex;"><span><span style="color:#66d9ef">GET</span> _cat<span style="color:#a6e22e">/</span>indices
</span></span></code></pre></div></li>
</ul>
<h3 id="删除索引">删除索引</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elm" data-lang="elm"><span style="display:flex;"><span><span style="color:#66d9ef">DELETE</span> czh<span style="color:#ae81ff">20</span>
</span></span></code></pre></div><h2 id="操作类型表">操作类型（表）</h2>
<h3 id="创建类型">创建类型</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">PUT</span> <span style="color:#960050;background-color:#1e0010">czh</span><span style="color:#ae81ff">20</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&#34;mappings&#34;</span>: {
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;users&#34;</span>: {
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">&#34;properties&#34;</span>: {
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;id&#34;</span>: {
</span></span><span style="display:flex;"><span>          <span style="color:#f92672">&#34;type&#34;</span>: <span style="color:#e6db74">&#34;long&#34;</span>
</span></span><span style="display:flex;"><span>        },
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;name&#34;</span>: {
</span></span><span style="display:flex;"><span>          <span style="color:#f92672">&#34;type&#34;</span>: <span style="color:#e6db74">&#34;keyword&#34;</span>
</span></span><span style="display:flex;"><span>        },
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;age&#34;</span>: {
</span></span><span style="display:flex;"><span>          <span style="color:#f92672">&#34;type&#34;</span>: <span style="color:#e6db74">&#34;integer&#34;</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>      }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h3 id="查看插入的类型">查看插入的类型</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">GET</span> <span style="color:#960050;background-color:#1e0010">czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">/mapping</span>
</span></span></code></pre></div><h2 id="操作文档数据">操作文档（数据）</h2>
<h3 id="添加文档">添加文档</h3>
<blockquote>
<p>注意：</p>
<ul>
<li>当索引/类型/索引/映射不存在时，会自动添加</li>
<li>ES中的数据一般时从其他数据库导入的，所以文档的ID会沿用原始数据库中的ID</li>
<li>添加时，如果指定文档的ID已经存在，则执行更新操作，否则是添加操作</li>
<li>不指定ID的添加操作，ES会添加一个字符串类型的ID</li>
</ul>
</blockquote>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">POST</span> <span style="color:#960050;background-color:#1e0010">/czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">/users/</span><span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;id&#34;</span>:<span style="color:#ae81ff">1001</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;name&#34;</span>:<span style="color:#e6db74">&#34;张三&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;age&#34;</span>:<span style="color:#ae81ff">20</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p><code>POST /czh20/users/1</code>表示在<strong>czh20</strong>中的<strong>users</strong>中添加<strong>第1</strong>条数据</p>
<h3 id="更新文档">更新文档</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">POST</span> <span style="color:#960050;background-color:#1e0010">/czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">/users/</span><span style="color:#ae81ff">1</span><span style="color:#960050;background-color:#1e0010">/_update</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;doc&#34;</span>:{
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;name&#34;</span>:<span style="color:#e6db74">&#34;陈治桦&#34;</span>
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p><code>POST /czh20/users/1/_update</code>表示修改<strong>czh20</strong>中的<strong>users</strong>中的<strong>第1</strong>条数据</p>
<h3 id="查看文档">查看文档</h3>
<p><strong>注意：</strong></p>
<ul>
<li>右侧显示最多只能显示十条数据，不代表只查出来十条数据</li>
</ul>
<h4 id="根据id查询">根据ID查询</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">GET</span> <span style="color:#960050;background-color:#1e0010">/czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">/users/</span><span style="color:#ae81ff">1</span>
</span></span></code></pre></div><h4 id="查询所有">查询所有</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">GET</span> <span style="color:#960050;background-color:#1e0010">/czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">/users/_search</span>
</span></span></code></pre></div><h3 id="删除文档">删除文档</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">DELETE</span> <span style="color:#960050;background-color:#1e0010">/czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">/users/</span><span style="color:#ae81ff">1</span>
</span></span></code></pre></div><h2 id="高级查询">高级查询</h2>
<h3 id="排序">排序</h3>
<p><strong>注意：</strong></p>
<p>如果不加排序，默认按**_score**降序排列</p>
<ul>
<li>
<p>需求：查询所有用户，按照<strong>id</strong>降序</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">GET</span> <span style="color:#960050;background-color:#1e0010">/czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">/users/_search</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;sort&#34;</span>:
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;id&#34;</span>:
</span></span><span style="display:flex;"><span>        {
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&#34;order&#34;</span>: <span style="color:#e6db74">&#34;desc&#34;</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>简写</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">GET</span> <span style="color:#960050;background-color:#1e0010">/czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">/users/_search</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;sort&#34;</span>:
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;id&#34;</span>:<span style="color:#e6db74">&#34;desc&#34;</span>
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div></li>
<li>
<p>多列排(当学号相同时，按年龄升序排列)</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">GET</span> <span style="color:#960050;background-color:#1e0010">/czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">/users/_search</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;sort&#34;</span>:[
</span></span><span style="display:flex;"><span>        {
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&#34;id&#34;</span>:{
</span></span><span style="display:flex;"><span>            	<span style="color:#f92672">&#34;order&#34;</span>: <span style="color:#e6db74">&#34;desc&#34;</span>
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>        },
</span></span><span style="display:flex;"><span>        {
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&#34;age&#34;</span>:{
</span></span><span style="display:flex;"><span>                <span style="color:#f92672">&#34;order&#34;</span>:<span style="color:#e6db74">&#34;asc&#34;</span>
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    ]
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div></li>
</ul>
<h3 id="分页">分页</h3>
<ul>
<li>
<p>格式：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;from&#34;</span>:<span style="color:#960050;background-color:#1e0010">start</span>, <span style="color:#75715e">//(currentPage-1)*pageSize
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#f92672">&#34;size&#34;</span>:<span style="color:#960050;background-color:#1e0010">pageSize</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div></li>
<li>
<p>需求：从用户列表第一条开始，每页显示3条数据，并按照<strong>id</strong>降序排列</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">GET</span> <span style="color:#960050;background-color:#1e0010">/czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">/users/_search</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;sort&#34;</span>:[
</span></span><span style="display:flex;"><span>        {
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&#34;id&#34;</span>:{
</span></span><span style="display:flex;"><span>                <span style="color:#f92672">&#34;order&#34;</span>:<span style="color:#e6db74">&#34;desc&#34;</span>
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    ]
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><pre><code>简写：
</code></pre>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">GET</span> <span style="color:#960050;background-color:#1e0010">/czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">/users/_search</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;sort&#34;</span>:{<span style="color:#f92672">&#34;id&#34;</span>:<span style="color:#e6db74">&#34;desc&#34;</span>},
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;from&#34;</span>:<span style="color:#ae81ff">0</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;size&#34;</span>:<span style="color:#ae81ff">3</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div></li>
</ul>
<h3 id="es搜索查询">ES搜索查询</h3>
<ul>
<li>term &ndash; 精准匹配  不会被分词器拆分</li>
<li>match &ndash; 全文检索  会被分词器拆分</li>
<li>range &ndash; 范围检索   gt gte lt lte</li>
</ul>
<blockquote>
<p><code>term</code>和<code>match</code>可以用在数值和字符上</p>
<p><code>range</code>只用用在数值上</p>
</blockquote>
<h4 id="term">term</h4>
<ul>
<li>
<p>需求：查询年龄为20的用户</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">GET</span> <span style="color:#960050;background-color:#1e0010">/czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">/users/_search</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;query&#34;</span>:{
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;term&#34;</span>:{
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&#34;age&#34;</span>:<span style="color:#ae81ff">20</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div></li>
<li>
<p>需求：查询年龄为20或21的用户</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">GET</span> <span style="color:#960050;background-color:#1e0010">/czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">/users/_search</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;query&#34;</span>:{
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;terms&#34;</span>:{
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&#34;age&#34;</span>:[<span style="color:#ae81ff">21</span>, <span style="color:#ae81ff">20</span>]
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div></li>
</ul>
<h4 id="range">range</h4>
<ul>
<li>
<p>需求：查询年龄 0-20之间的用户，并且排序</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">GET</span> <span style="color:#960050;background-color:#1e0010">/czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">/users/_search</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;query&#34;</span>:{
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;range&#34;</span>:{
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&#34;age&#34;</span>:{
</span></span><span style="display:flex;"><span>                <span style="color:#f92672">&#34;gte&#34;</span>:<span style="color:#ae81ff">0</span>,
</span></span><span style="display:flex;"><span>                <span style="color:#f92672">&#34;lte&#34;</span>:<span style="color:#ae81ff">20</span>
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    },
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;sort&#34;</span>:{
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;age&#34;</span>:{
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&#34;order&#34;</span>:<span style="color:#e6db74">&#34;desc&#34;</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div></li>
</ul>
<h4 id="match">match</h4>
<ul>
<li>
<p>需求：查询商品标题中含有“游戏 手机”</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">GET</span> <span style="color:#960050;background-color:#1e0010">/es_shop/shop_product/_search</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;query&#34;</span>:{
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;mmatch&#34;</span>:{
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&#34;title&#34;</span>:<span style="color:#e6db74">&#34;游戏 手机&#34;</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div></li>
<li>
<p>需求：查询商品标签或简介中含有“游戏 手机 蓝牙”模糊查询</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">GET</span> <span style="color:#960050;background-color:#1e0010">/es_shop/shop_product/_search</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;query&#34;</span>:{
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;multi_match&#34;</span>:{
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&#34;query&#34;</span>:<span style="color:#e6db74">&#34;游戏 手机 蓝牙&#34;</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&#34;fileds&#34;</span>:[<span style="color:#e6db74">&#34;title&#34;</span>, <span style="color:#e6db74">&#34;intro&#34;</span>]
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div></li>
</ul>
<h3 id="逻辑查询">逻辑查询</h3>
<blockquote>
<p>must/should/must_not &lt;=&gt; and/or/not</p>
</blockquote>
<ul>
<li>
<p>需求：查询商品标题含有i7并且价格大于8000</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">GET</span> <span style="color:#960050;background-color:#1e0010">/es_shop/shop_product/_search</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;query&#34;</span>:{
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;bool&#34;</span>:{
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&#34;must&#34;</span>:[
</span></span><span style="display:flex;"><span>                {<span style="color:#f92672">&#34;range&#34;</span>:{
</span></span><span style="display:flex;"><span>                    <span style="color:#f92672">&#34;price:{
</span></span></span><span style="display:flex;"><span><span style="color:#f92672">                    	&#34;</span><span style="color:#960050;background-color:#1e0010">gte</span><span style="color:#e6db74">&#34;:8000
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">                }
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">                }},
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">                {
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">                &#34;</span><span style="color:#960050;background-color:#1e0010">match</span><span style="color:#e6db74">&#34;:{
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">                	&#34;</span><span style="color:#960050;background-color:#1e0010">title</span><span style="color:#e6db74">&#34;:&#34;</span><span style="color:#960050;background-color:#1e0010">i</span><span style="color:#ae81ff">7</span><span style="color:#960050;background-color:#1e0010">&#34;</span>
</span></span><span style="display:flex;"><span>                }
</span></span><span style="display:flex;"><span>                }
</span></span><span style="display:flex;"><span>            ]
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div></li>
<li>
<p>需求：查询商品标题 pro 或者 价格 5000-8000</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">GET</span> <span style="color:#960050;background-color:#1e0010">/es_shop/shop_product/_search</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;query&#34;</span>:{
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;bool&#34;</span>:{
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&#34;should&#34;</span>:[
</span></span><span style="display:flex;"><span>                {<span style="color:#f92672">&#34;range&#34;</span>:{
</span></span><span style="display:flex;"><span>                    <span style="color:#f92672">&#34;price&#34;</span>:{
</span></span><span style="display:flex;"><span>                        <span style="color:#f92672">&#34;gte&#34;</span>:<span style="color:#ae81ff">5000</span>,
</span></span><span style="display:flex;"><span>                        <span style="color:#f92672">&#34;lte&#34;</span>:<span style="color:#ae81ff">8000</span>
</span></span><span style="display:flex;"><span>                    }
</span></span><span style="display:flex;"><span>                }},
</span></span><span style="display:flex;"><span>                {
</span></span><span style="display:flex;"><span>                    <span style="color:#f92672">&#34;match&#34;</span>:{
</span></span><span style="display:flex;"><span>                        <span style="color:#f92672">&#34;title&#34;</span>:<span style="color:#e6db74">&#34;pro&#34;</span>
</span></span><span style="display:flex;"><span>                    }
</span></span><span style="display:flex;"><span>                }
</span></span><span style="display:flex;"><span>            ]
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div></li>
</ul>
<h3 id="分词器-1">分词器</h3>
<blockquote>
<p><code>ik_max_word</code>：细力度</p>
<p><code>ik_smart</code>：粗力度</p>
</blockquote>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">GET</span> <span style="color:#960050;background-color:#1e0010">/czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">/_analyze</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;text&#34;</span>:<span style="color:#e6db74">&#34;I am Groot&#34;</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">GET</span> <span style="color:#960050;background-color:#1e0010">/czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">/_analyze</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;text&#34;</span>:<span style="color:#e6db74">&#34;英特网酷睿i7处理器&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;analyzer&#34;</span>:<span style="color:#e6db74">&#34;ik_smart&#34;</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">GET</span> <span style="color:#960050;background-color:#1e0010">/czh</span><span style="color:#ae81ff">20</span><span style="color:#960050;background-color:#1e0010">/_analyze</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;text&#34;</span>:<span style="color:#e6db74">&#34;英特网酷睿i7处理器&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;analyzer&#34;</span>:<span style="color:#e6db74">&#34;ik_max_word&#34;</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h3 id="高亮显示">高亮显示</h3>
<blockquote>
<p>将关键词加上<code>CSS</code>样式标签即可</p>
</blockquote>
<ul>
<li>
<p>查询商品标题或者简介含有“蓝牙  指纹  双卡”并且高亮显示</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">GET</span> <span style="color:#960050;background-color:#1e0010">/es_shop/shop_product/_search</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;query&#34;</span>:{
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;multi_match&#34;</span>: {
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&#34;query&#34;</span>:<span style="color:#e6db74">&#34;蓝牙 指纹 双卡&#34;</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&#34;fields&#34;</span>:[<span style="color:#e6db74">&#34;title&#34;</span>, <span style="color:#e6db74">&#34;intro&#34;</span>]
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    },
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;highlight&#34;</span>:{
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;fields&#34;</span>:{
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&#34;title&#34;</span>:{},
</span></span><span style="display:flex;"><span>            <span style="color:#f92672">&#34;intro&#34;</span>:{}
</span></span><span style="display:flex;"><span>        },
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;pre_tags&#34;</span>:<span style="color:#e6db74">&#34;&lt;span style=&#39;color:red&#39;&gt;&#34;</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&#34;post_tags&#34;</span>:<span style="color:#e6db74">&#34;&lt;/&#34;</span>
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div></li>
</ul>

    <h4><a href="https://czh-dev.gitee.io/czh-blog.gitee.io/">Back to Home</a></h4>
  </div>

  <div class="span3 bs-docs-sidebar" style="position:fixed;right: 40px;top: 50px;">
    <h1>catalogue</h1>
    <ul class="nav nav-list bs-docs-sidenav">
      <div class="toc-div">
        <nav id="TableOfContents">
  <ul>
    <li><a href="#简介">简介</a>
      <ul>
        <li><a href="#主要组件">主要组件</a></li>
        <li><a href="#分片和副本">分片和副本</a></li>
        <li><a href="#分词器">分词器</a></li>
      </ul>
    </li>
    <li><a href="#操作索引相当于数据库">操作索引（相当于数据库）</a>
      <ul>
        <li><a href="#添加索引">添加索引</a></li>
        <li><a href="#查询索引">查询索引</a></li>
        <li><a href="#删除索引">删除索引</a></li>
      </ul>
    </li>
    <li><a href="#操作类型表">操作类型（表）</a>
      <ul>
        <li><a href="#创建类型">创建类型</a></li>
        <li><a href="#查看插入的类型">查看插入的类型</a></li>
      </ul>
    </li>
    <li><a href="#操作文档数据">操作文档（数据）</a>
      <ul>
        <li><a href="#添加文档">添加文档</a></li>
        <li><a href="#更新文档">更新文档</a></li>
        <li><a href="#查看文档">查看文档</a></li>
        <li><a href="#删除文档">删除文档</a></li>
      </ul>
    </li>
    <li><a href="#高级查询">高级查询</a>
      <ul>
        <li><a href="#排序">排序</a></li>
        <li><a href="#分页">分页</a></li>
        <li><a href="#es搜索查询">ES搜索查询</a></li>
        <li><a href="#逻辑查询">逻辑查询</a></li>
        <li><a href="#分词器-1">分词器</a></li>
        <li><a href="#高亮显示">高亮显示</a></li>
      </ul>
    </li>
  </ul>
</nav>
      </div>
    </ul>
  </div>

</div>
<script src="https://cdn.jsdelivr.net/npm/gumshoejs@5.1.2/dist/gumshoe.min.js"></script>
<script>
  var spy = new Gumshoe('#TableOfContents a', {
    nested: true,
    nestedClass: 'active'
  });
</script>
<style>
   
  #TableOfContents li,
  #TableOfContents ul {
    list-style-type: none;
  }

  #TableOfContents ul {
    padding-left: 0px;
  }

  #TableOfContents li>a {
    display: block;
    padding: 4px 20px;
    font-size: 95%;
    color: #000000;
  }

  #TableOfContents li>a:hover,
  #TableOfContents li>a:focus {
    padding-left: 19px;
    color: #3A6bA5;
    text-decoration: none;
    background-color: transparent;
    border-left: 1px solid #3A6bA5;
  }

  #TableOfContents li.active>a,
  #TableOfContents li.active>a:hover,
  #TableOfContents li.active>a:focus {
    padding-left: 18px;
    font-weight: bold;
    color: #3A6bA5;
    background-color: transparent;
    border-left: 2px solid #3A6bA5;
  }

   
  #TableOfContents li>ul {
    padding-bottom: 10px;
  }

  #TableOfContents li li>a {
    padding-top: 1px;
    padding-bottom: 1px;
    padding-left: 30px;
    font-size: 14px;
    font-weight: normal;
  }

  #TableOfContents li li>a:hover,
  #TableOfContents li li>a:focus {
    padding-left: 29px;
  }

  #TableOfContents li li.active>a,
  #TableOfContents li li.active>a:hover,
  #TableOfContents li li.active>a:focus {
    padding-left: 28px;
    font-weight: 500;
  }

  #TableOfContents .nav-link.active+ul {
    display: block;
  }

  #TableOfContents li>ul {
    display: none;
  }

  #TableOfContents li.active>ul {
    display: inherit;
  }

  .toc-div {
    position: -webkit-sticky;
     
    position: sticky;
     
    top: 20px;
  }
</style>


        </div><footer class="container">
    <hr class="soften">
    <p>
    <a href="https://space.bilibili.com/1799809923">Love eating fried pork ribs</a> | 

&copy; 
<a href="http://jmf-portfolio.netlify.com" target="_blank">
    JM Fergeau
</a>
<span id="thisyear">2023</span>

    | My site


        | Built on <a href="//gohugo.io" target="_blank">Hugo</a>

</p>
    <p class="text-center">
        <a href="https://facebook.com">Facebook</a> 
        <a href="https://twitter.com">Twitter</a> 
        <a href="https://linkedin.com">Linkedin</a> 
        <a href="https://github.com">GitHub</a> 
        <a href="https://gitlab.com">GitLab</a>
    </p>
</footer>

</body><link rel="stylesheet" href="/czh-blog.gitee.io/css/bootstrap.css">
<link rel="stylesheet" href="/czh-blog.gitee.io/css/bootstrap-responsive.css">
<link rel="stylesheet" href="/czh-blog.gitee.io/css/style.css">

<script src="/czh-blog.gitee.io/js/jquery.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-386.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-transition.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-alert.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-modal.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-dropdown.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-scrollspy.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-tab.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-tooltip.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-popover.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-button.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-collapse.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-carousel.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-typeahead.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-affix.js"></script>
<script>
    _386 = { 
        fastLoad: false ,
        onePass: false , 
        speedFactor: 1 
    };

    
    function ThisYear() {
        document.getElementById('thisyear').innerHTML = new Date().getFullYear();
    };
</script>
</html>
